package com.sip.stream.utils.zfy.yuv;

import android.media.Image;
import android.os.SystemClock;

import com.sip.stream.utils.ImageYUVBean;

import java.nio.ByteBuffer;

public class NV12ImageReader extends ICamera2ImageReader {
    public NV12ImageReader(int width, int height) {
        super(width, height);
    }

    @Override
    public ImageYUVBean getDataFormImage(Image image) {
        byte[] obtainOutputData = obtainOutputData(this.dataLength);
        try {
            ByteBuffer buffer = image.getPlanes()[0].getBuffer();
            ByteBuffer buffer2 = image.getPlanes()[1].getBuffer();
            ByteBuffer buffer3 = image.getPlanes()[2].getBuffer();
            buffer.get(obtainOutputData, 0, this.ySize);
            buffer3.get(obtainOutputData, this.ySize, buffer3.remaining());
            obtainOutputData[this.dataLength - 1] = buffer2.get(buffer2.remaining() - 1);
            ImageYUVBean imageYUVBean = new ImageYUVBean();
            imageYUVBean.setCameraData(obtainOutputData);
            imageYUVBean.setImageTimestampus(SystemClock.elapsedRealtimeNanos() / 1000);
            return imageYUVBean;
        } catch (Exception e) {
            e.printStackTrace();
            recycle(obtainOutputData);
            return null;
        }
    }
}
